package kuang.juc.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;


public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test01();   //24943
        //test02();   //18169
        //test03();       //941
    }
    //普通for循环
    public static void test01(){
        long start = System.currentTimeMillis();
        Long sum=0L;
        for (Long i = 1L; i < 100_0000_0000L; i++) {
            sum+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println("结果"+sum+"==="+(end-start));
    }

    //forkjoin
    public static void test02() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> submit = forkJoinPool.submit(new ForkJoinDemo(0L, 100_0000_0000L));
        Long sum = submit.get();

        long end = System.currentTimeMillis();
        System.out.println("结果"+sum+"==="+(end-start));
    }

    //stream并行流
    public static void test03(){
        long start = System.currentTimeMillis();
        //long sum = LongStream.rangeClosed(0L, 10_0000_0000L).sum();
        long sum = LongStream.range(0L, 100_0000_0000L).parallel().sum();
        long end = System.currentTimeMillis();
        System.out.println("结果"+sum+"==="+(end-start));
    }
}
